﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class VayNganHang : System.Web.UI.Page
{
    int pc;
    SqlParameter[] p;
    clsXuLy ex = new clsXuLy();
    string frmName = "frmvaynganhang";
    static int f_SuDung = 0, f_Them = 0, f_Sua = 0, f_Xoa = 0, f_XemTatCa = 0, f_InBaoCao = 0;
    public string Header = System.Configuration.ConfigurationSettings.AppSettings["Header"].ToString();
    static DataTable dt;
    static int PageNumber = 6;
    static int CurrentPage = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Convert.ToString(Session["user"])))
            gstGetMess("Hết phiên làm việc, vui lòng đăng nhập để tiếp tục", "Default.aspx");
        else
        {
            if (!IsPostBack)
            {


                string idnguoidung = Session["IDUser"].ToString();
                string sql = "select SuDung,Them,Sua,Xoa,InBaoCao,XemTatCa from XDAPhanQuyen where IDnguoidung='" + idnguoidung + "' and tag='" + frmName + "'";

                DataTable dt2 = ex.GetData_Text(sql);
                if (dt2.Rows.Count > 0)
                {
                    f_SuDung = int.Parse(dt2.Rows[0]["SuDung"].ToString());
                    f_Them = int.Parse(dt2.Rows[0]["Them"].ToString());
                    f_Sua = int.Parse(dt2.Rows[0]["Sua"].ToString());
                    f_Xoa = int.Parse(dt2.Rows[0]["Xoa"].ToString());
                    f_InBaoCao = int.Parse(dt2.Rows[0]["InBaoCao"].ToString());
                    f_XemTatCa = int.Parse(dt2.Rows[0]["XemTatCa"].ToString());
                }
                if (f_SuDung.Equals(1))
                {
                    CurrentPage = 1;
                    btnCancel_Click(sender, e);

                    Title = "Quản lý vay ngân hàng | " + Header;

                    btnCancel_Click(sender, e);
                    LoadBenVay();
                    LoadNguoiChoVay();
                    FirstGridViewRow();
                    LoadKyHanVay();
                    FirstGridViewRow1();
                    FirstGridViewRow2();
                }
                else
                    gstGetMess("Bạn không có quyền xem trang này", "Home.aspx");

            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (f_Them.Equals(1))
        {
            try
            {
                Guid id = Guid.NewGuid();
                p = new SqlParameter[11];
                p[0] = new SqlParameter("@IDs", id);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                p[1] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                p[1].SqlDbType = SqlDbType.SmallDateTime;
                p[2] = new SqlParameter("@NganHang", ex.GetGuid(drnguoichovay.SelectedValue));
                p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                p[3] = new SqlParameter("@BenVay", ex.GetGuid(drbenvay.SelectedValue));
                p[3].SqlDbType = SqlDbType.UniqueIdentifier;
                p[4] = new SqlParameter("@DonVi", txtdonvi.Text.Trim());
                p[4].SqlDbType = SqlDbType.NVarChar;
                p[5] = new SqlParameter("@SoTien", double.Parse(txtsotien.Text.Trim().Replace(",", "").Equals("") ? "0" : txtsotien.Text.Trim()));
                p[5].SqlDbType = SqlDbType.Float;
                p[6] = new SqlParameter("@ThoiHanVay", double.Parse(txtthoihanvay.Text.Trim().Replace(",", "").Equals("") ? "0" : txtthoihanvay.Text.Trim()));
                p[6].SqlDbType = SqlDbType.Float;
                p[7] = new SqlParameter("@KiHanVay", ex.GetGuid(drkyhan.SelectedValue));
                p[7].SqlDbType = SqlDbType.UniqueIdentifier;
                p[8] = new SqlParameter("@KiTraLai", double.Parse(txtkitralai.Text.Trim().Replace(",", "").Equals("") ? "0" : txtkitralai.Text.Trim()));
                p[8].SqlDbType = SqlDbType.Float;
                p[9] = new SqlParameter("@KiTraGoc", double.Parse(txtkitragoc.Text.Trim().Replace(",", "").Equals("") ? "0" : txtkitragoc.Text.Trim()));
                p[9].SqlDbType = SqlDbType.Float;
                p[10] = new SqlParameter("@Del", 1);
                p[10].SqlDbType = SqlDbType.Int;
                ex.ExecuteNonQuerry("sp_xdavaynganhang_Add", p);

                for (int i = 0; i < GV1.Rows.Count; i++)
                {
                    TextBox txtphantramlai = (TextBox)GV1.Rows[i].Cells[1].FindControl("txtphantramlai");
                    TextBox tungay = (TextBox)GV1.Rows[i].Cells[2].FindControl("tungay");
                    TextBox denngay = (TextBox)GV1.Rows[i].Cells[3].FindControl("denngay");
                    p = new SqlParameter[6];
                    p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[1] = new SqlParameter("@PhanTramLai", double.Parse(txtphantramlai.Text.Trim().Replace(",", "").Equals("") ? "0" : txtphantramlai.Text.Trim()));
                    p[1].SqlDbType = SqlDbType.Float;
                    p[2] = new SqlParameter("@TuNgay", ex.GetNgayThang(tungay.Text.Trim()));
                    p[2].SqlDbType = SqlDbType.SmallDateTime;
                    p[3] = new SqlParameter("@DenNgay", ex.GetNgayThang(denngay.Text.Trim()));
                    p[3].SqlDbType = SqlDbType.SmallDateTime;
                    p[4] = new SqlParameter("@IDVay", id);
                    p[4].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[5] = new SqlParameter("@Del", 1);
                    p[5].SqlDbType = SqlDbType.Int;
                    ex.ExecuteNonQuerry("sp_xdalainganhang_Add", p);
                }

                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    TextBox txtnoidung = (TextBox)GridView1.Rows[i].Cells[1].FindControl("txtnoidung");
                    
                    p = new SqlParameter[4];
                    p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[1] = new SqlParameter("@NoiDung", txtnoidung.Text.Trim());
                    p[1].SqlDbType = SqlDbType.NVarChar;
                  
                    p[2] = new SqlParameter("@IDVay", id);
                    p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[3] = new SqlParameter("@Del", 1);
                    p[3].SqlDbType = SqlDbType.Int;
                    ex.ExecuteNonQuerry("sp_xdanoidungvaynganhang_Add", p);
                }

                for (int i = 0; i < GridView2.Rows.Count; i++)
                {
                    TextBox txttaisanthechap = (TextBox)GridView2.Rows[i].Cells[1].FindControl("txttaisanthechap");

                    p = new SqlParameter[4];
                    p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[1] = new SqlParameter("@TaiSan", txttaisanthechap.Text.Trim());
                    p[1].SqlDbType = SqlDbType.NVarChar;

                    p[2] = new SqlParameter("@IDVay", id);
                    p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[3] = new SqlParameter("@Del", 1);
                    p[3].SqlDbType = SqlDbType.Int;
                    ex.ExecuteNonQuerry("sp_xdataisanvaynganhang_Add", p);
                }
                //Guid tlchi = Guid.NewGuid();
                //p = new SqlParameter[9];
                //p[0] = new SqlParameter("@IDs", tlchi);
                //p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                //p[1] = new SqlParameter("@NhanVien", ex.GetGuid(drbenvay.SelectedValue));
                //p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                //p[2] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                //p[2].SqlDbType = SqlDbType.SmallDateTime;
                //p[3] = new SqlParameter("@SoTien", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                //p[3].SqlDbType = SqlDbType.Float;
                //p[4] = new SqlParameter("@NoiDung", DBNull.Value);
                //p[4].SqlDbType = SqlDbType.NVarChar;

                //p[5] = new SqlParameter("@NgayTao", DateTime.Now);
                //p[5].SqlDbType = SqlDbType.SmallDateTime;
                //p[6] = new SqlParameter("@NguoiTao", Session["user"].ToString());
                //p[6].SqlDbType = SqlDbType.NVarChar;
                //p[7] = new SqlParameter("@Del", 1);
                //p[7].SqlDbType = SqlDbType.Int;
                //p[8] = new SqlParameter("@IDThanhToan", id);
                //p[8].SqlDbType = SqlDbType.UniqueIdentifier;
                //ex.ExecuteNonQuerry("sp_XDAThietLapPhieuThuTienMat_Add", p);




                //p = new SqlParameter[4];
                //p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                //p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                //p[1] = new SqlParameter("@NoiDung", "Vay tiền mặt");
                //p[1].SqlDbType = SqlDbType.NVarChar;
                //p[2] = new SqlParameter("@IDPhieuThu", tlchi);
                //p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                //p[3] = new SqlParameter("@Del", 1);
                //p[3].SqlDbType = SqlDbType.Int;
                //ex.ExecuteNonQuerry("sp_xdanoidungthutienmat_Add", p);


                //p = new SqlParameter[1];
                //p[0] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                //p[0].SqlDbType = SqlDbType.SmallDateTime;
                //dt = ex.GetData("sp_xdagetsophieuthuconnotravat_TienMat", p);
                //int sopc = Int16.Parse(dt.Rows[0]["sopc"].ToString());
                //if (sopc == 0)
                //    pc = 1;
                //else pc = sopc + 1;
                //p = new SqlParameter[11];
                //p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                //p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                //p[1] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                //p[1].SqlDbType = SqlDbType.SmallDateTime;
                //p[2] = new SqlParameter("@NoiDung", DBNull.Value);
                //p[2].SqlDbType = SqlDbType.NVarChar;
                //p[3] = new SqlParameter("@DonVi", DBNull.Value);
                //p[3].SqlDbType = SqlDbType.UniqueIdentifier;
                //p[4] = new SqlParameter("@SoPC", pc);
                //p[4].SqlDbType = SqlDbType.Int;
                //p[5] = new SqlParameter("@TaiKhoan", DBNull.Value);
                //p[5].SqlDbType = SqlDbType.UniqueIdentifier;
                //p[6] = new SqlParameter("@Del", 1);
                //p[6].SqlDbType = SqlDbType.Int;
                //p[7] = new SqlParameter("@LoaiCongNo", 2);
                //p[7].SqlDbType = SqlDbType.Int;
                //p[8] = new SqlParameter("@SoTien", double.Parse(txtsotien.Text.Trim().Equals("") ? "0" : txtsotien.Text.Trim()));
                //p[8].SqlDbType = SqlDbType.Float;
                //p[9] = new SqlParameter("@IDLenhChi", tlchi);
                //p[9].SqlDbType = SqlDbType.UniqueIdentifier;
                //p[10] = new SqlParameter("@NhanVien", ex.GetGuid(drbenvay.SelectedValue));
                //p[10].SqlDbType = SqlDbType.UniqueIdentifier;
                //ex.ExecuteNonQuerry("sp_XDAPhieuChiCongNoThu_Add", p);

                btnCancel_Click(sender, e);
                gstGetMess("Lưu thành công", "");
            }
            catch (Exception ax)
            {
                gstGetMess("Có lỗi trong quá trình lưu, xem trong file log", "");
                ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tVay tiền mặt\t" + ax.Message);
            }
        }
        else
            gstGetMess("Bạn không có quyền thêm dữ liệu trên trang này", "");
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        FirstGridViewRow1();
        FirstGridViewRow2();
        FirstGridViewRow();
        drnguoichovay.SelectedIndex = 0;
        txtngaythang.Text = "";
        txtsotien.Text = "";
        txtngaythang.Text = "";
        txtdonvi.Text = "";
        drbenvay.SelectedIndex = 0;
        p = new SqlParameter[2];
        GV.DataSource = ex.GetData("sp_xdavaynganhang_List");
        GV.DataBind();
        btnSave.Visible = true;
        btncapnhat.Visible = false;
        btnTraTienGoc.Visible = false;
        btnTraTienLai.Visible = false;
    }
    private void gstGetMess(string gstMess, string gstLink)
    {
        if (gstLink == "")
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "')", true);
        else
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "');window.location.href='" + gstLink + "'", true);

    }
    protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string ID = e.CommandArgument.ToString();
        hdID.Value = ID;
        Guid gID = new Guid(ID);
        p = new SqlParameter[1];
        p[0] = new SqlParameter("@IDs", gID);
        p[0].SqlDbType = SqlDbType.UniqueIdentifier;

        if (e.CommandName.Equals("Select"))
        {
            DataTable dt = ex.GetData("sp_xdavaynganhang_ListByID", p);
            if (dt.Rows.Count > 0)
            {
                try
                {

                    txtsotien.Text = dt.Rows[0]["sotien"].ToString();
                    txtngaythang.Text = dt.Rows[0]["ngaythang"].ToString();
                    drnguoichovay.Text = dt.Rows[0]["nganhang"].ToString();
                    drbenvay.Text = dt.Rows[0]["benvay"].ToString();
                    txtdonvi.Text = dt.Rows[0]["donvi"].ToString();
                    txtthoihanvay.Text = dt.Rows[0]["thoihanvay"].ToString();
                    txtkitralai.Text = dt.Rows[0]["kitralai"].ToString();
                    txtkitragoc.Text = dt.Rows[0]["kitragoc"].ToString();
                }
                catch (Exception ax)
                {
                    gstGetMess("Có lỗi trong quá trình xem, xem trong file log", "");
                    ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tSố dư tiền mặt\t" + ax.Message);
                }

            }

            int rowIndex = 0;
            p = new SqlParameter[1];
            p[0] = new SqlParameter("@IDs", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            dt = ex.GetData("sp_xdavaynganhang_getLai", p);
            if (ViewState["CurrentTable"] != null)
            {

                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        TextBox txtphantramlai = (TextBox)GV1.Rows[rowIndex].Cells[1].FindControl("txtphantramlai");
                        TextBox tungay = (TextBox)GV1.Rows[rowIndex].Cells[2].FindControl("tungay");
                        TextBox denngay = (TextBox)GV1.Rows[rowIndex].Cells[3].FindControl("denngay");
                        GV1.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                        txtphantramlai.Focus();
                        txtphantramlai.Text = dt.Rows[i]["PhanTramLai"].ToString();
                        tungay.Text = dt.Rows[i]["TuNgay"].ToString();
                        denngay.Text = dt.Rows[i]["DenNgay"].ToString();
                        rowIndex++;
                        if (txtphantramlai.Text != null)
                        {
                            AddNewRow();
                        }
                    }
                }
                SetRowData();

              

            }

            else
            {
                Response.Write("ViewState is null");
            }


            int rowIndex1 = 0;
            p = new SqlParameter[1];
            p[0] = new SqlParameter("@IDs", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            dt = ex.GetData("sp_xdavaynganhang_getnoidung", p);
            if (ViewState["CurrentTable1"] != null)
            {

                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        TextBox txtnoidung = (TextBox)GridView1.Rows[rowIndex1].Cells[1].FindControl("txtnoidung");
                       
                        GV1.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                        txtnoidung.Focus();
                        txtnoidung.Text = dt.Rows[i]["noidung"].ToString();
                        
                        rowIndex++;
                        
                    }
                }
                
            }
                
            else
            {
                Response.Write("ViewState is null");
            }


            int rowIndex2 = 0;
            p = new SqlParameter[1];
            p[0] = new SqlParameter("@IDs", gID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            dt = ex.GetData("sp_xdavaynganhang_gettaisan", p);
            if (ViewState["CurrentTable2"] != null)
            {

                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        TextBox txtphantramlai = (TextBox)GridView2.Rows[rowIndex2].Cells[1].FindControl("txttaisanthechap");

                        GV1.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                        txtphantramlai.Focus();
                        txtphantramlai.Text = dt.Rows[i]["taisan"].ToString();

                        rowIndex++;
                        
                    }
                }

            }

            else
            {
                Response.Write("ViewState is null");
            }

            btncapnhat.Visible = true;
            btnSave.Visible = false;
        }
        
        else
            if (e.CommandName.Equals("Del"))
            {
                if (f_Xoa.Equals(1))
                {
                    try
                    {
                        p = new SqlParameter[1];
                        p[0] = new SqlParameter("@IDs", gID);
                        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                        ex.ExecuteNonQuerry("sp_xdavaynganhang_Del", p);
                        p = new SqlParameter[1];
                        p[0] = new SqlParameter("@IDs", gID);
                        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                        ex.ExecuteNonQuerry("sp_xdalainganhang_Del", p);
                        p = new SqlParameter[1];
                        p[0] = new SqlParameter("@IDs", gID);
                        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                        ex.ExecuteNonQuerry("sp_xdanoidungvaynganhang_Del", p);
                        p = new SqlParameter[1];
                        p[0] = new SqlParameter("@IDs", gID);
                        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                        ex.ExecuteNonQuerry("sp_xdataisanvaynganhang_Del", p);
                       
                        btnCancel_Click(sender, e);
                        gstGetMess("Xóa thành công", "");
                    }
                    catch (Exception ax)
                    {
                        gstGetMess("Có lỗi trong quá trình xóa, xem trong file log", "");
                        ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tVay tiền mặt\t" + ax.Message);

                    }
                }
                else
                    gstGetMess("Bạn không có quyền xóa dữ liệu trên trang này", "");
            }
    }
    protected void btncapnhat_Click(object sender, EventArgs e)
    {
        if (f_Sua.Equals(1))
        {
            try
            {
                Guid gID = new Guid(hdID.Value);
                p = new SqlParameter[1];
                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                ex.ExecuteNonQuerry("sp_xdalainganhang_Del", p);
                p = new SqlParameter[1];
                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                ex.ExecuteNonQuerry("sp_xdanoidungvaynganhang_Del", p);
                p = new SqlParameter[1];
                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                ex.ExecuteNonQuerry("sp_xdataisanvaynganhang_Del", p);
                       
               
               

                for (int i = 0; i < GV1.Rows.Count; i++)
                {
                    TextBox txtphantramlai = (TextBox)GV1.Rows[i].Cells[1].FindControl("txtphantramlai");
                    TextBox tungay = (TextBox)GV1.Rows[i].Cells[2].FindControl("tungay");
                    TextBox denngay = (TextBox)GV1.Rows[i].Cells[3].FindControl("denngay");
                    if (txtphantramlai.Text != "")
                    {
                        p = new SqlParameter[6];
                        p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                        p[1] = new SqlParameter("@PhanTramLai", double.Parse(txtphantramlai.Text.Trim().Replace(",", "").Equals("") ? "0" : txtphantramlai.Text.Trim()));
                        p[1].SqlDbType = SqlDbType.Float;
                        p[2] = new SqlParameter("@TuNgay", ex.GetNgayThang(tungay.Text.Trim()));
                        p[2].SqlDbType = SqlDbType.SmallDateTime;
                        p[3] = new SqlParameter("@DenNgay", ex.GetNgayThang(denngay.Text.Trim()));
                        p[3].SqlDbType = SqlDbType.SmallDateTime;
                        p[4] = new SqlParameter("@IDVay", gID);
                        p[4].SqlDbType = SqlDbType.UniqueIdentifier;
                        p[5] = new SqlParameter("@Del", 1);
                        p[5].SqlDbType = SqlDbType.Int;
                        ex.ExecuteNonQuerry("sp_xdalainganhang_Add", p);
                    }
                }

                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    TextBox txtnoidung = (TextBox)GridView1.Rows[i].Cells[1].FindControl("txtnoidung");
                    if (txtnoidung.Text != "")
                    {
                        p = new SqlParameter[4];
                        p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                        p[1] = new SqlParameter("@NoiDung", txtnoidung.Text.Trim());
                        p[1].SqlDbType = SqlDbType.NVarChar;

                        p[2] = new SqlParameter("@IDVay", gID);
                        p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                        p[3] = new SqlParameter("@Del", 1);
                        p[3].SqlDbType = SqlDbType.Int;
                        ex.ExecuteNonQuerry("sp_xdanoidungvaynganhang_Add", p);
                    }
                }

                for (int i = 0; i < GridView2.Rows.Count; i++)
                {
                    TextBox txttaisanthechap = (TextBox)GridView2.Rows[i].Cells[1].FindControl("txttaisanthechap");
                    if (txttaisanthechap.Text != "")
                    {
                        p = new SqlParameter[4];
                        p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                        p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                        p[1] = new SqlParameter("@TaiSan", txttaisanthechap.Text.Trim());
                        p[1].SqlDbType = SqlDbType.NVarChar;

                        p[2] = new SqlParameter("@IDVay", gID);
                        p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                        p[3] = new SqlParameter("@Del", 1);
                        p[3].SqlDbType = SqlDbType.Int;
                        ex.ExecuteNonQuerry("sp_xdataisanvaynganhang_Add", p);
                    }
                }

                p = new SqlParameter[11];
                p[0] = new SqlParameter("@IDs", gID);
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;

                p[1] = new SqlParameter("@NgayThang", ex.GetNgayThang(txtngaythang.Text.Trim()));
                p[1].SqlDbType = SqlDbType.SmallDateTime;
                p[2] = new SqlParameter("@NganHang", ex.GetGuid(drnguoichovay.SelectedValue));
                p[2].SqlDbType = SqlDbType.UniqueIdentifier;
                p[3] = new SqlParameter("@BenVay", ex.GetGuid(drbenvay.SelectedValue));
                p[3].SqlDbType = SqlDbType.UniqueIdentifier;
                p[4] = new SqlParameter("@DonVi", txtdonvi.Text.Trim());
                p[4].SqlDbType = SqlDbType.NVarChar;
                p[5] = new SqlParameter("@SoTien", double.Parse(txtsotien.Text.Trim().Replace(",", "").Equals("") ? "0" : txtsotien.Text.Trim()));
                p[5].SqlDbType = SqlDbType.Float;
                p[6] = new SqlParameter("@ThoiHanVay", double.Parse(txtthoihanvay.Text.Trim().Replace(",", "").Equals("") ? "0" : txtthoihanvay.Text.Trim()));
                p[6].SqlDbType = SqlDbType.Float;
                p[7] = new SqlParameter("@KiHanVay", ex.GetGuid(drkyhan.SelectedValue));
                p[7].SqlDbType = SqlDbType.UniqueIdentifier;
                p[8] = new SqlParameter("@KiTraLai", double.Parse(txtkitralai.Text.Trim().Replace(",", "").Equals("") ? "0" : txtkitralai.Text.Trim()));
                p[8].SqlDbType = SqlDbType.Float;
                p[9] = new SqlParameter("@KiTraGoc", double.Parse(txtkitragoc.Text.Trim().Replace(",", "").Equals("") ? "0" : txtkitragoc.Text.Trim()));
                p[9].SqlDbType = SqlDbType.Float;
                p[10] = new SqlParameter("@Del", 1);
                p[10].SqlDbType = SqlDbType.Int;
                ex.ExecuteNonQuerry("sp_xdavaynganhang_Update", p);
                btnCancel_Click(sender, e);
                gstGetMess("Cập nhật thành công", "");
            }
            catch (Exception ax)
            {
                gstGetMess("Có lỗi trong quá trình cập nhật, xem trong file log", "");
                ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tSố dư tiền mặt\t" + ax.Message);
            }
        }
        else
            gstGetMess("Bạn không có quyền sửa dữ liệu trên trang này", "");
    }

    void LoadNguoiChoVay()
    {
        drnguoichovay.Items.Clear();
        drnguoichovay.DataSource = ex.GetData("sp_xdabenvaytiemat_List");
        drnguoichovay.DataBind();
        drnguoichovay.Items.Insert(0, new ListItem("--Chọn người cho vay--", ""));
        drnguoichovay.SelectedIndex = 0;
    }
    void LoadBenVay()
    {
        drbenvay.Items.Clear();
        drbenvay.DataSource = ex.GetData("sp_xdabenvaytiemat_ListNhanVien");
        drbenvay.DataBind();
        drbenvay.Items.Insert(0, new ListItem("--Chọn bên vay--", ""));
        drbenvay.SelectedIndex = 0;
    }

    void LoadKyHanVay()
    {
        drkyhan.Items.Clear();
        drkyhan.DataSource = ex.GetData("sp_xdavaynganhang_ListKiHan");
        drkyhan.DataBind();
        drkyhan.Items.Insert(0, new ListItem("--Chọn kì hạn vay--", ""));
        drkyhan.SelectedIndex = 0;
    }

    private void FirstGridViewRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("PhanTramLai", typeof(string)));
        dt.Columns.Add(new DataColumn("tungay", typeof(string)));
        dt.Columns.Add(new DataColumn("denngay", typeof(string)));
        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["PhanTramLai"] = string.Empty;
        dr["tungay"] = string.Empty;
        dr["denngay"] = string.Empty;
        dt.Rows.Add(dr);

        ViewState["CurrentTable"] = dt;


        GV1.DataSource = dt;
        GV1.DataBind();
    }
    protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        SetRowData();
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            int rowIndex = Convert.ToInt32(e.RowIndex);
            if (dt.Rows.Count > 1)
            {
                dt.Rows.Remove(dt.Rows[rowIndex]);
                drCurrentRow = dt.NewRow();
                ViewState["CurrentTable"] = dt;
                GV1.DataSource = dt;
                GV1.DataBind();

                for (int i = 0; i < GV1.Rows.Count - 1; i++)
                {
                    GV1.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                }
                SetPreviousData();
            }
        }
    }
    private void SetRowData()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    TextBox txtphantramlai = (TextBox)GV1.Rows[rowIndex].Cells[1].FindControl("txtphantramlai");
                    TextBox tungay = (TextBox)GV1.Rows[rowIndex].Cells[2].FindControl("tungay");
                    TextBox denngay = (TextBox)GV1.Rows[rowIndex].Cells[3].FindControl("denngay");
                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;
                    dtCurrentTable.Rows[i - 1]["PhanTramLai"] = txtphantramlai.Text.Trim();
                    dtCurrentTable.Rows[i - 1]["tungay"] = tungay.Text.Trim();
                    dtCurrentTable.Rows[i - 1]["denngay"] = denngay.Text.Trim();
                    rowIndex++;
                }

                ViewState["CurrentTable"] = dtCurrentTable;
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }
    }
    private void AddNewRow()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    TextBox txtphantramlai = (TextBox)GV1.Rows[rowIndex].Cells[1].FindControl("txtphantramlai");
                    TextBox tungay = (TextBox)GV1.Rows[rowIndex].Cells[2].FindControl("tungay");
                    TextBox denngay = (TextBox)GV1.Rows[rowIndex].Cells[3].FindControl("denngay");

                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;
                    dtCurrentTable.Rows[i - 1]["PhanTramLai"] = txtphantramlai.Text.Trim();
                    dtCurrentTable.Rows[i - 1]["tungay"] = tungay.Text.Trim();
                    dtCurrentTable.Rows[i - 1]["denngay"] = denngay.Text.Trim();
                    rowIndex++;
                }
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTable"] = dtCurrentTable;

                GV1.DataSource = dtCurrentTable;
                GV1.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }
        SetPreviousData();

    }
    private void SetPreviousData()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TextBox txtphantramlai = (TextBox)GV1.Rows[rowIndex].Cells[1].FindControl("txtphantramlai");
                    TextBox tungay = (TextBox)GV1.Rows[rowIndex].Cells[2].FindControl("tungay");
                    TextBox denngay = (TextBox)GV1.Rows[rowIndex].Cells[3].FindControl("denngay");
                    GV1.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                    txtphantramlai.Focus();
                    txtphantramlai.Text = dt.Rows[i]["PhanTramLai"].ToString();
                    tungay.Text = dt.Rows[i]["tungay"].ToString();
                    denngay.Text = dt.Rows[i]["denngay"].ToString();

                    rowIndex++;
                }
            }
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        AddNewRow();
    }
    protected void GV_PreRender(object sender, EventArgs e)
    {

        for (int rowIndex = GV.Rows.Count - 2; rowIndex >= 0; rowIndex--)
        {
            GridViewRow gvRow = GV.Rows[rowIndex];
            GridViewRow gvPreviousRow = GV.Rows[rowIndex + 1];
            //for (int cellCount = 0; cellCount < 4; cellCount++)
            //{
            int cellCount = 0;
            if (gvRow.Cells[2].Text.Equals(gvPreviousRow.Cells[2].Text) && gvRow.Cells[3].Text.Equals(gvPreviousRow.Cells[3].Text) && gvRow.Cells[4].Text.Equals(gvPreviousRow.Cells[4].Text) && gvRow.Cells[5].Text.Equals(gvPreviousRow.Cells[5].Text) && gvRow.Cells[6].Text.Equals(gvPreviousRow.Cells[6].Text) && gvRow.Cells[7].Text.Equals(gvPreviousRow.Cells[7].Text))
            {

                if ((!gvRow.Cells[cellCount].Text.Equals("&nbsp;")) && (!gvPreviousRow.Cells[cellCount].Text.Equals("&nbsp;")))
                {
                    if (gvRow.Cells[cellCount].Text == gvPreviousRow.Cells[cellCount].Text)
                    {
                        if (gvPreviousRow.Cells[cellCount].RowSpan < 2)
                        {
                            gvRow.Cells[0].RowSpan = 2;
                            gvRow.Cells[1].RowSpan = 2;
                            gvRow.Cells[2].RowSpan = 2;
                            gvRow.Cells[3].RowSpan = 2;
                            gvRow.Cells[4].RowSpan = 2;
                            gvRow.Cells[5].RowSpan = 2;
                            gvRow.Cells[6].RowSpan = 2;
                            gvRow.Cells[7].RowSpan = 2;
                        }
                        else
                        {
                            gvRow.Cells[0].RowSpan =
                                gvPreviousRow.Cells[0].RowSpan + 1;
                            gvRow.Cells[1].RowSpan =
                               gvPreviousRow.Cells[1].RowSpan + 1;
                            gvRow.Cells[2].RowSpan =
                               gvPreviousRow.Cells[2].RowSpan + 1;
                            gvRow.Cells[3].RowSpan =
                              gvPreviousRow.Cells[3].RowSpan + 1;
                            gvRow.Cells[4].RowSpan =
                            gvPreviousRow.Cells[4].RowSpan + 1;
                            gvRow.Cells[5].RowSpan =
                           gvPreviousRow.Cells[5].RowSpan + 1;
                            gvRow.Cells[6].RowSpan =
                           gvPreviousRow.Cells[6].RowSpan + 1;
                            gvRow.Cells[7].RowSpan =
                           gvPreviousRow.Cells[7].RowSpan + 1;
                        }
                        gvPreviousRow.Cells[0].Visible = false;
                        gvPreviousRow.Cells[1].Visible = false;
                        gvPreviousRow.Cells[2].Visible = false;
                        gvPreviousRow.Cells[3].Visible = false;
                        gvPreviousRow.Cells[4].Visible = false;
                        gvPreviousRow.Cells[5].Visible = false;
                        gvPreviousRow.Cells[6].Visible = false;
                        gvPreviousRow.Cells[7].Visible = false;

                    }
                }
            }

            //}

        }
    }
    private void AddNewRow1()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable1"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable1"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    TextBox txtnoidung = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("txtnoidung");

                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;
                    dtCurrentTable.Rows[i - 1]["Noidung"] = txtnoidung.Text.Trim();

                    rowIndex++;
                }
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTable1"] = dtCurrentTable;

                GridView1.DataSource = dtCurrentTable;
                GridView1.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }
        SetPreviousData1();

    }
    private void SetPreviousData1()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable1"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable1"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TextBox txtnoidung = (TextBox)GridView1.Rows[rowIndex].Cells[1].FindControl("txtnoidung");

                    GridView1.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                    txtnoidung.Focus();
                    txtnoidung.Text = dt.Rows[i]["Noidung"].ToString();



                    rowIndex++;
                }
            }
        }
    }



    private void FirstGridViewRow1()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("Noidung", typeof(string)));

        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["Noidung"] = string.Empty;

        dt.Rows.Add(dr);

        ViewState["CurrentTable1"] = dt;


        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    protected void gv_RowDeleting1(object sender, GridViewDeleteEventArgs e)
    {
        SetRowData();
        if (ViewState["CurrentTable1"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable1"];
            DataRow drCurrentRow = null;
            int rowIndex = Convert.ToInt32(e.RowIndex);
            if (dt.Rows.Count > 1)
            {
                dt.Rows.Remove(dt.Rows[rowIndex]);
                drCurrentRow = dt.NewRow();
                ViewState["CurrentTable1"] = dt;
                GridView1.DataSource = dt;
                GridView1.DataBind();

                for (int i = 0; i < GridView1.Rows.Count - 1; i++)
                {
                    GridView1.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                }
                SetPreviousData1();
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        AddNewRow1();
    }

    private void AddNewRow2()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable2"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable2"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    TextBox txtnoidung = (TextBox)GridView2.Rows[rowIndex].Cells[1].FindControl("txttaisanthechap");

                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;
                    dtCurrentTable.Rows[i - 1]["TaiSanTheChap"] = txtnoidung.Text.Trim();

                    rowIndex++;
                }
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTable2"] = dtCurrentTable;

                GridView2.DataSource = dtCurrentTable;
                GridView2.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }
        SetPreviousData2();

    }
    private void SetPreviousData2()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable2"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable2"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TextBox txtnoidung = (TextBox)GridView2.Rows[rowIndex].Cells[1].FindControl("txttaisanthechap");

                    GridView2.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                    txtnoidung.Focus();
                    txtnoidung.Text = dt.Rows[i]["TaiSanTheChap"].ToString();



                    rowIndex++;
                }
            }
        }
    }



    private void FirstGridViewRow2()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("TaiSanTheChap", typeof(string)));

        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["TaiSanTheChap"] = string.Empty;

        dt.Rows.Add(dr);

        ViewState["CurrentTable2"] = dt;


        GridView2.DataSource = dt;
        GridView2.DataBind();
    }
    protected void gv_RowDeleting2(object sender, GridViewDeleteEventArgs e)
    {
        SetRowData();
        if (ViewState["CurrentTable2"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable2"];
            DataRow drCurrentRow = null;
            int rowIndex = Convert.ToInt32(e.RowIndex);
            if (dt.Rows.Count > 1)
            {
                dt.Rows.Remove(dt.Rows[rowIndex]);
                drCurrentRow = dt.NewRow();
                ViewState["CurrentTable2"] = dt;
                GridView2.DataSource = dt;
                GridView2.DataBind();

                for (int i = 0; i < GridView2.Rows.Count - 1; i++)
                {
                    GridView2.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                }
                SetPreviousData2();
            }
        }
    }

   
   
    protected void Button2_Click(object sender, EventArgs e)
    {
        AddNewRow2();
    }
}